<?php

namespace App\Admin\Forms;

use App\Library\Util\SignUtil;
use Encore\Admin\Widgets\Form;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Http;

class Activity extends Form
{
    /**
     * The form title.
     *
     * @var string
     */
    public $title = '活动查询';

    /**
     * Handle the form request.
     *
     * @param Request $request
     *
     * @return \Illuminate\Http\RedirectResponse
     */
    public function handle(Request $request)
    {
        //dump($request->all());
        $url = $request->input('url');
        if (empty($url)) {
            admin_error("请输入活动链接");
            return back();
        }
        $array = parse_url($url);
        if (empty($array['host'])) {
            admin_error("请输入有效活动链接");
            return back();
        }

        if ($array['host'] != 'comt.cib.com.cn') {
            admin_error("仅支持查询https://comt.cib.com.cn域名银行活动");
            return back();
        }
        $query = $array['query'];
        parse_str($query, $a);
        if (empty($a) || empty($a['content']) || empty($a['activityNo'])) {
            admin_error("活动链接参数错误");
            return back();
        }
        $activityNo = $a['activityNo'];
        $content = $a['content'];
        $req = [
            'requestType'  => '1',
            'activityType' => '1',
            'activityNo'   => $activityNo,
            'contentType'  => '',
            'content'      => $content,
        ];
        $timestamp = time() * 1000;
        $signData = $req;
        $signData['timestamp'] = $timestamp;
        $sign = SignUtil::getInstance()->buildLzActivitySign($signData);
        $result = Http::withHeaders([
            'sign'      => $sign,
            'timestamp' => $timestamp,
        ])->post('https://comt.cib.com.cn/activity/api/coinMgm/coinMgmActivity', $req)->json();
        if (empty($result)) {
            admin_error("请求接口失败，请再次重试。多次失败后请联系技术支持排查");
            return back();
        }
        $detail = $result['data']['data'] ?? [];
        if (empty($detail)) {
            admin_error("活动详情接口未返回有效数据");
            return back();
        }
        $remark = $result['code'] != "0000" ? $result['message'] : "获取信息成功";
        $channelName = $detail['channelName'] ?? '未知渠道';
        $activityName = $detail['activityName'] ?? '未知活动';
        $createOpName = $detail['createOpName'];
        $createTime = $detail['createTime'];

        $res = '渠道名称：【' . $channelName . '】';
        $res .= PHP_EOL . '活动编号：【' . $activityNo . '】';
        $res .= PHP_EOL . '活动名称：【' . $activityName . '】';
        $res .= PHP_EOL . '创建人姓名：【' . $createOpName . '】';
        $res .= PHP_EOL . '创建时间：【' . $createTime . '】';
        $res .= PHP_EOL . '备注：【' . $remark . '】';
        return back()->with(['result' => $res]);
    }

    /**
     * Build a form here.
     */
    public function form()
    {
        $this->textarea('url', __("活动链接"))->rules('required|url', [
            'required' => '请输入活动链接',
            'url'      => '活动链接不合法',
        ]);

    }

    /**
     * The data of the form.
     *
     * @return array $data
     */
    public function data()
    {
        return [

        ];
    }
}
